package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.ProfTitleDao;
import cn.edu.sdjzu.xg.bysj.domain.Degree;
import cn.edu.sdjzu.xg.bysj.domain.ProfTitle;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class ProfTitleService {
	private static ProfTitleDao profTitleDao= ProfTitleDao.getInstance();
	private static ProfTitleService profTitleService=new ProfTitleService();
	private ProfTitleService(){}

	public static ProfTitleService getInstance(){
		return profTitleService;
	}

	public Collection<ProfTitle> findAll(List conditionList,
										 Pagination pagination) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Collection<ProfTitle> profTitles =
				ProfTitleDao.findAll(conditionList,pagination,connection) ;
		JdbcHelper.close(connection);
		return profTitles;
	}

	public ProfTitle find(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		ProfTitle profTitle = profTitleDao.find(id,connection);
		JdbcHelper.close(connection);
		return profTitle;
	}

	public boolean update(ProfTitle profTitle) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean updated =false;
		try {
			//增加用户
			updated = profTitleDao.update(profTitle,connection);
			//提交事务
			connection.commit();

		}catch (Exception e){
			//回滚
			connection.rollback();

		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return updated ;
	}

	public boolean add(ProfTitle profTitle) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean added =false;
		try {
			//增加用户
			added = profTitleDao.add(profTitle,connection);
			//提交事务
			connection.commit();

		}catch (Exception e){
			//回滚
			connection.rollback();

		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return added ;
	}
	public boolean delete(Integer id) throws SQLException{
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean deleted =false;
		try {
			//增加用户
			deleted = profTitleDao.delete(id,connection);
			//提交事务
			connection.commit();

		}catch (Exception e){
			//回滚
			connection.rollback();

		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return deleted ;
	}
}

